package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.org.util.EmployeeQuery;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //部门经理查询
    @PatchMapping("/manageAll")
    public List<Employee> findManageAll(){

        return employeeService.findManageAll();
    }
    //高级查询
    @PatchMapping
    public PageInfo<Employee> loadAll(@RequestBody EmployeeQuery employeeQuery){
        System.out.println(employeeQuery);
        return employeeService.findAll(employeeQuery);
    }
    //添加
    @PutMapping
    public AjaxResult add(@RequestBody Employee employee){
        try {
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }
    //添加
    @PostMapping
    public AjaxResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }
    //单个删除
    @DeleteMapping("{id}")
    public AjaxResult oneDelete(@PathVariable("id") Long id){
        try {
            employeeService.oneDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }
    //批量删除
    @PatchMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Employee> employees){
        try {
            employeeService.batchRemove(employees);
            return new AjaxResult();
        } catch (Exception e) {
          return new AjaxResult(false,e.getMessage());
        }
    }
}
